package com.gitee.chili.leisure.service.impl;

import com.gitee.chili.leisure.domain.Account;
import com.gitee.chili.leisure.domain.Currency;
import com.gitee.chili.leisure.enums.CurrencyToken;
import com.gitee.chili.leisure.repository.AccountRepository;
import com.gitee.chili.leisure.service.IAccountService;
import com.gitee.chili.leisure.service.ICurrencyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * AccountService
 *
 * @author week solider
 * @since 2020.09.01
 */
@Slf4j
@Service
public class AccountService implements IAccountService {


    @Autowired
    private AccountRepository accountRepository;

    @Autowired
    private ICurrencyService currencyService;

    @Override
    public AccountRepository getRepository() {
        return accountRepository;
    }

    @Override
    public Optional<Account> getBalance(CurrencyToken token, Long memberId) {

        // 获取币种
        Currency currency = new Currency().setToken(token);

        Optional<Currency> optional = currencyService.getRepository().findOne(Example.of(currency));

        if(optional.isPresent()){
            currency = optional.get();
        }

        Account account = new Account().setMemberId(memberId).setCurrency(currency);

        return accountRepository.findOne(Example.of(account));
    }
}
